package pe.edu.upc.dsd.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import pe.edu.upc.dsd.domain.Hambur;
import pe.edu.upc.dsd.domain.HamburLista;
import pe.edu.upc.dsd.util.Writer;

import javax.servlet.http.HttpServletResponse;


@Controller
public class HamburController {

	protected static Logger logger = Logger.getLogger("controller");
	
	private RestTemplate restTemplate = new RestTemplate();
	
	@RequestMapping(value = "/getphoto", method = RequestMethod.GET)
	public void getPhoto(@RequestParam("id") Long id, HttpServletResponse response) {
		logger.debug("Retrieve photo with id: " + id);
		
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.IMAGE_JPEG);
		
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<String> entity = new HttpEntity<String>(headers);	
		
		ResponseEntity<byte[]> result = restTemplate.exchange("http://localhost:8080/Valdez/pan/hambur/{id}", HttpMethod.GET, entity, byte[].class, id);
		
		Writer.write(response, result.getBody());
	}
	
	@RequestMapping(value = "/getall", method = RequestMethod.GET)
	public String getAll(Model model) {
		logger.debug("consultando");

		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);

		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<Hambur> entity = new HttpEntity<Hambur>(headers);

		try {
			ResponseEntity<HamburLista> result = restTemplate.exchange("http://localhost:8080/Valdez/pan/hambures", HttpMethod.GET, entity, HamburLista.class);
	
			model.addAttribute("hambures", result.getBody().getData());
			
		} catch (Exception e) {
			logger.error(e);
		}

		return "hamburpage";
	}

	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public String getHambur(@RequestParam("id") Long id, Model model) {
		logger.debug("Retrieve with id: " + id);
		
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);
		
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<Hambur> entity = new HttpEntity<Hambur>(headers);

		try {
			ResponseEntity<Hambur> result = restTemplate.exchange("http://localhost:8080/Valdez/pan/hambur/{id}", HttpMethod.GET, entity, Hambur.class, id);
			model.addAttribute("hambur", result.getBody());
			
		} catch (Exception e) {
			logger.error(e);
		}
				return "getpage";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String getAddPage(Model model) {
		logger.debug("Received request to show add page");

		model.addAttribute("hamburAttribute", new Hambur());

		return "addpage";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addHambur(@ModelAttribute("hamburAttribute") Hambur hambur,
						 Model model) {
		logger.debug("Add new ");

		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);

		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);

		HttpEntity<Hambur> entity = new HttpEntity<Hambur>(hambur, headers);

		try {
			ResponseEntity<Hambur> result = restTemplate.exchange("http://localhost:8080/Valdez/pan/hambur", HttpMethod.POST, entity, Hambur.class);
		} catch (Exception e) {
			logger.error(e);
		}
	
		return "resultpage";
	}

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String getUpdatePage(@RequestParam(value="id", required=true) Integer id,  
    										Model model) {
    	logger.debug("Received request to show edit page");

		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);

		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<Hambur> entity = new HttpEntity<Hambur>(headers);

		try {
			ResponseEntity<Hambur> result = restTemplate.exchange("http://localhost:8080/Valdez/pan/hambur/{id}", HttpMethod.GET, entity, Hambur.class, id);
			model.addAttribute("hamburAttribute", result.getBody());
			
		} catch (Exception e) {
			logger.error(e);
		}

    	return "updatepage";
	}
	

    @RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateHambur(@ModelAttribute("hamburAttribute") Hambur hambur,
						@RequestParam(value="id",  required=true) Long id,
						Model model) {
		logger.debug("Update existing");
		

		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);

		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);

		HttpEntity<Hambur> entity = new HttpEntity<Hambur>(hambur, headers);
		

		ResponseEntity<String> result = restTemplate.exchange("http://localhost:8080/Valdez/pan/hambur/{id}", HttpMethod.PUT, entity, String.class, id);


		return "resultpage";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String deleteHambur(@RequestParam("id") Long id,
										Model model) {
    	logger.debug("Delete existing ");
    	

		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_XML);

    	HttpHeaders headers = new HttpHeaders();
		headers.setAccept(acceptableMediaTypes);
		HttpEntity<String> entity = new HttpEntity<String>(headers);	
		

		ResponseEntity<String> result = restTemplate.exchange("http://localhost:8080/Valdez/pan/hambur/{id}", HttpMethod.DELETE, entity, String.class, id);

		return "resultpage";
    }
}
